package com.facade;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.common.constant.DateConstant;
import com.dao.MemberDAO;
import com.model.Member;
import com.model.MembershipPayment;
import com.objects.MemberSearch;

public class MemberFacade implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private MemberDAO memberDAO = new MemberDAO();

	public void createMember(Member member) {
		memberDAO.beginTransaction();
		memberDAO.save(member);
		memberDAO.commitAndCloseTransaction();
	}
	
	public Member findMemberWithNRIC(String nric) {
		memberDAO.beginTransaction();
		Member member = memberDAO.findMemberWithNRIC(nric);
		memberDAO.closeTransaction();
		return member;
	}
	
	public Member findActiveMemberWithName(String name) {
		memberDAO.beginTransaction();
		Member member = memberDAO.findActiveMemberWithName(name);
		memberDAO.closeTransaction();
		return member;
	}	
	
	public List<Member> listAll() {
		memberDAO.beginTransaction();
		List<Member> result = memberDAO.findAll();
		memberDAO.closeTransaction();

		return result;
	}
	
	public List<Member> listActiveMembers() {
		memberDAO.beginTransaction();
		List<Member> result = memberDAO.listActive();
		memberDAO.closeTransaction();

		return result;
	}		
	
	public void updateMember(Member member) {
		memberDAO.beginTransaction();
		Member persistedMember = memberDAO.find(member.getId());
		persistedMember.setName(member.getName());
		persistedMember.setNric(member.getNric());
		persistedMember.setDob(member.getDob());
		persistedMember.setGender(member.getGender());
		persistedMember.setAddress1(member.getAddress1());
		persistedMember.setAddress2(member.getAddress2());
		persistedMember.setAddress3(member.getAddress3());
		persistedMember.setEmail(member.getEmail());
		persistedMember.setMobileNo(member.getMobileNo());
		persistedMember.setOccupation(member.getOccupation());
		persistedMember.setEmergencyName(member.getEmergencyName());
		persistedMember.setEmergencyContactNo(member.getEmergencyContactNo());
		persistedMember.setRelationship(member.getRelationship());
		persistedMember.setMedicals(member.getMedicals());
		persistedMember.setUpdatedBy(member.getUpdatedBy());
		persistedMember.setUpdatedDate(member.getUpdatedDate());
		memberDAO.commitAndCloseTransaction();
	}	
	
	public void suspendTerminateMember(Member member) {
		memberDAO.beginTransaction();
		Member persistedMember = memberDAO.find(member.getId());
		persistedMember.setSuspend(member.isSuspend());
		persistedMember.setActive(member.isActive());
		persistedMember.setUpdatedBy(member.getUpdatedBy());
		persistedMember.setUpdatedDate(member.getUpdatedDate());
		memberDAO.commitAndCloseTransaction();
	}	
	
	public void changeExpiryMember(Member member) {
		memberDAO.beginTransaction();
		Member persistedMember = memberDAO.find(member.getId());
		persistedMember.setMembership(member.getMembership());
		persistedMember.setMembershipExpiryDate(member.getMembershipExpiryDate());
		persistedMember.setUpdatedBy(member.getUpdatedBy());
		persistedMember.setUpdatedDate(member.getUpdatedDate());
		memberDAO.commitAndCloseTransaction();
	}		
	
	public void extendMembership(MembershipPayment membershipPayment){
		MemberDAO memberDAO = new MemberDAO();
		memberDAO.beginTransaction();
		int memberId = membershipPayment.getMember().getId();
		Member persistedMember = memberDAO.find(memberId);
		extendMembershipExpiry(persistedMember);
		persistedMember.setMembershipLastPaymentDate(new Date());
		memberDAO.commitAndCloseTransaction();
	}	
	
	public void extendMembershipExpiry(Member member){
		Date expiryDate = member.getMembershipExpiryDate();
		Calendar c = Calendar.getInstance();
		c.setTime(expiryDate);
		c.add(Calendar.MONTH, member.getMembership().getMonthValidity());
		c.add(Calendar.DATE, 1);
		Date newExpiryDate = c.getTime();
		member.setMembershipExpiryDate(newExpiryDate);
	}	
	
	public List<Member> search(MemberSearch memberSearch){
		
		Date dobFrom = memberSearch.getDobFrom();
		if(dobFrom == null){
			dobFrom = DateConstant.MIN_DATE;
		}
		
		Date dobTo = memberSearch.getDobTo();
		if(dobTo == null){
			dobTo = DateConstant.MAX_DATE;
		}
		
		Date expiryFrom = memberSearch.getExpiryFrom();
		if(expiryFrom == null){
			expiryFrom = DateConstant.MIN_DATE;
		}
		
		Date expiryTo = memberSearch.getExpiryTo();
		if(expiryTo == null){
			expiryTo = DateConstant.MAX_DATE;
		}
		
		Date paymentFrom = memberSearch.getLastPaymentFrom();
		if(paymentFrom == null){
			paymentFrom = DateConstant.MIN_DATE;
		}
		
		Date paymentTo = memberSearch.getLastPaymentTo();
		if(paymentTo == null){
			paymentTo = DateConstant.MAX_DATE;
		}
		
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("name", "%"+memberSearch.getName()+"%");
		parameters.put("nric", "%"+memberSearch.getNric()+"%");
		parameters.put("dobFrom", dobFrom);
		parameters.put("dobTo", dobTo);
		parameters.put("mobile", "%"+memberSearch.getMobileNo()+"%");
		parameters.put("email", "%"+memberSearch.getEmail()+"%");
		parameters.put("expiryFrom", expiryFrom);
		parameters.put("expiryTo", expiryTo);
		parameters.put("lastPaymentFrom", paymentFrom);
		parameters.put("lastPaymentTo", paymentTo);
		parameters.put("suspended", memberSearch.isSuspended());
		parameters.put("terminate", memberSearch.isActive());
		
		memberDAO.beginTransaction();
		List<Member> resultList = memberDAO.search(parameters);
		memberDAO.closeTransaction();
		
		return resultList;
	}

}
